package com.phonebook.controller;

import com.phonebook.entity.PhoneBook;
import com.phonebook.entity.ResponseResult;
import com.phonebook.service.PhoneBookService;
import com.phonebook.util.PinYinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 电话簿Controller类
 *
 * @author 高旭
 */
@RestController
@RequestMapping("/phoneBook")
@Api(tags = "电话簿记录Api")
public class PhoneBookController {

    @Autowired
    private PhoneBookService phoneBookService;

    /**
     * 查询所有电话簿信息
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/loadAll")
    @ApiOperation("查询所有电话簿信息")
    public ResponseResult loadAll() {
        Map<String, Object> map = new LinkedHashMap<>();
        List<Character> letters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '#');
        letters.forEach(letter -> {
            List<PhoneBook> phoneBooks = phoneBookService.loadByInitial(String.valueOf(letter));
            if (phoneBooks.size() > 0) {
                map.put(String.valueOf(letter), phoneBooks);
            }
        });
        return ResponseResult.success(map);
    }


    /**
     * 添加或者修改通讯记录
     *
     * @param phoneBook
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    @ApiOperation("添加或者修改通讯记录")
    @ApiImplicitParam(name = "phoneBook", value = "电话簿记录", dataTypeClass = PhoneBook.class, required = true)
    public ResponseResult save(@RequestBody PhoneBook phoneBook) {
        int resultTotal = 0;
        String initial = String.valueOf(PinYinUtil.getPinYin(phoneBook.getName()).charAt(0)).toUpperCase();
        if (StringUtils.isAlpha(initial)) {
            phoneBook.setInitial(initial);
        } else {
            phoneBook.setInitial("#");
        }
        if (phoneBook.getId() == null) {
            resultTotal = phoneBookService.add(phoneBook);
        } else {
            resultTotal = phoneBookService.update(phoneBook);
        }
        if (resultTotal > 0) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error("保存失败，请联系管理员");
        }
    }

    /**
     * 根据id查询电话簿信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询电话簿信息")
    @ApiImplicitParam(name = "id", value = "电话簿记录编号", dataTypeClass = Integer.class, required = true)
    public ResponseResult findById(Integer id) {
        return ResponseResult.success(new HashMap<String, Object>(16) {{
            put("phoneBook", phoneBookService.findById(id));
        }});
    }

    /**
     * 根据id删除记录
     *
     * @param id
     * @return
     * @throws Exception
     */
    @DeleteMapping("/delete")
    @ApiOperation("根据id删除记录")
    @ApiImplicitParam(name = "id", value = "电话簿记录编号", dataTypeClass = Integer.class, required = true)
    public ResponseResult delete(Integer id) {
        int resultTotal = phoneBookService.delete(id);
        if (resultTotal < 1) {
            return ResponseResult.error("删除失败，请联系管理员");
        }
        return ResponseResult.success();
    }
}
